# import data_read
'''
data_2 = data_read.data_2
print("success")
'''
import numpy as np
import pandas as pd


interval_4 = [[4200, 4294],
              [4294, 4510],
              [4510, 4950],
              [4950, 5400],
              [5400, 6030],
              [6030, 7370],
              [7370, 9998]]

# 读取数据

data_4 = pd.read_excel(io='adjuncts/adjunct_4.xlsx', sheet_name="近红外", index_col=0)    # 读取附件四内容
print("data_4 read success!")
print(data_4)
# 数据清洗  去除问题中检材编号  去除产地信息


def peak_interval(left, right, list_row, start):  # 计算区间峰值位置 参数按照检材编号,左闭右开

    lst = list_row[left - start: right - start]          # 切割传入函数数据   list_row是一个list
    # print("===== * =====")
    lst_new = np.ndarray.tolist(lst)             # 数据类型转换
    peak = max(lst_new)                           # 计算峰值
    if peak > 0:
        return [lst_new.index(peak) + left, peak]    # 返回峰值x, y轴坐标
    else:
        return [0, 0]                                # 出现较大干扰值  返回状态码


# =========预处理数据=======

def data_pretreatment(data, interval, start):

    table = []                                            # 二维列表 用来存放预处理数据
    for r in data.index:
        row = data.loc[r].values[1:]             # 按行读取 附件 数据
        print(row)
        if pd.isnull(row[0]):
            print("++++++++++++++", r)
            continue
        result_row = []                                        # 存放该行数据处理结果
        for i in interval:                                     # 设i变量为区间库滚动游标
            t = peak_interval(i[0], i[1], row, start)                 # t接收返回的峰值 横纵坐标
            result_row.append(t[0])
            result_row.append(t[1])
        table.append(result_row)                          # 将该行数据存入表中
    return table


# print(data_2)

tables = data_pretreatment(data_4, interval_4, 4004 - 2)   # 减去op列占的列数
print(tables)   # 检验



